define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reportgoods/index',
                    add_url: 'reportgoods/add',
                    edit_url: 'reportgoods/edit',
                    del_url: 'reportgoods/del',
                    multi_url: 'reportgoods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");
            var identity = table.attr('identity');

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'goods_title', title: __('商品名称')},
                        {field: 'brand_title', title: __('品牌')},
                        {field: 'cate_title', title: __('分类')},
                        {field: 'purchases_num', title: __('配货量')},
                        {field: 'sale_num', title: __('销量')},
                        {field: 'sale_percent', title: __('售出比例')},
                        {field: 'profit', title: __('利润')},
                        {field: 'profit_rate', title: __('利润率')},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        }
                    ]
                ]
            });
            //分配用户
            $(document).on("click", ".btn-xq", function () {
                var ids = $(this).data("id");
                var url = $(this).data("url");
                var title = $(this).data("title");
                Backend.api.open(url + '/ids/' + ids, title, {area: ["90%", "90%"]}, {
                    callback: function (value) {
                        table.bootstrapTable('refresh', {});
                    },
                });

            });

            if (identity != 0) {
                $('#table').bootstrapTable('hideColumn', 'profit');
                $('#table').bootstrapTable('hideColumn', 'profit_rate');
            }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () { // 初始化表格参数配置
            var table = $("#table");
            var ids = table.attr('ids');
            Table.api.init({
                extend: {
                    index_url: 'reportgoods/detail?ids=' + ids,
                    table: 'goods',
                }
            });



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                pageSize: 100,
                queryParams: function (params) {
                    params.limit = 100;
                    return params;
                },
                columns: [
                    [
                        {field: 'day', title: __('时间')},
                        {field: 'purchases_num', title: __('配货量')},
                        {field: 'sale_num', title: __('销量')},
                        {field: 'sale_percent', title: __('售出比例')}
                    ]
                ]
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    var html = '-';
                    if (row.reply) {

                    } else {
                        html = '<a href="javascript:;" class="text-info btn-xq" data-title="商品详情" data-id="' + row.goods_id + '" data-url="reportgoods/detail">商品详情</a>';

                    }
                    return html;
                }
            },
        }
    };
    return Controller;
});